<?
include("sqlconnect.php");
session_start();

$curuser = $_SESSION['usrid'];

if( isset($_POST['ld']))
$parameter = $_POST['ld'];
else echo" Ajax Error ";


if($parameter == 'viewaproduct')
{
	
		$selected_product = $_POST['prod']; 
		
		$query= "SELECT * FROM product where product_name = '".$selected_product."'";
		$result= mysql_query($query) or die(mysql_error());		

		$row = mysql_fetch_object($result);

		$p_name = $row->product_name;
		$p_price = $row->product_price;
		$p_vendor = $row->vendor_name;
		$p_brand = $row->brand_name;
		$p_id = $row->product_id;

		/*$query= "SELECT * FROM user_history where product_name = '".$p_id."' AND user_id = $cur_user";
		$result= mysql_query($query) or die(mysql_error());		
		
		$already = mysql_num_rows($result);*/
		
		$time = time();
			
		$query1 = "INSERT INTO  user_history(user_id,
		product_id,
		hit_time
		)
		VALUES ('$curuser','$p_id','$time')";

		$result1= mysql_query($query1) or die(mysql_error());
		
		//echo $curuser.'#'.$p_id.'#'.$time;


		echo $p_name.'#'.$p_price.'#'.$p_vendor.'#'.$p_brand.'#'.$p_id;

		/*echo '

		<?xml version="1.0"?>
 		<product>
       	<name>'.$p_name.'</name>
       	<price>'.$p_price.'</price>
       	<vendor>'.$p_vendor.'</vendor>
       	<brand>'.$p_brand.'</brand>
        </product>

		';*/
}


if($parameter == 'addtowishlist')
{
		
		$p_id = $_POST['pid'];
		$time = time();

		$query = "INSERT INTO  user_fav(
		user_id,
		product_id,
		added_on,
		rating	
		)
		VALUES (
		'$curuser',  '$p_id',  '$time','0')";

		$result= mysql_query($query) or die(mysql_error());

		//echo $query;
}


if($parameter == 'loadcomments')
{
	
	$p_id = $_POST['pid'];
	$query = "SELECT * FROM user_comments where product_id = '".$p_id."'";
	$result = mysql_query($query) or die(mysql_error());
	
	
	$c_id = 0;
	while ($row = mysql_fetch_object($result))
	{
		 $c_user[$c_id] = $row->user_id;
		 $c_time[$c_id] = $row->comment_time;
		 $c_date[$c_id] = $row->comment_date;
		 $c_com[$c_id] = $row->comment_text;			
		 $query1= "SELECT * FROM users where user_id = '".$c_user[$c_id]."'";
		 $result1= mysql_query($query1) or die(mysql_error());
		 $row1 = mysql_fetch_object($result1);
		 $c_fname[$c_id] = $row1->user_fname;
		
	
	echo ' 
	<div id="c'.$c_id.'" class ="commentbox">
	<div id="commentor'.$c_id.'" class ="commentor" >
	<span id="'.$c_user[$c_id].'" class="namedisplay">'.$c_fname[$c_id].':</span><br><br>
	</div>
	<div id="commentcontent'.$c_id.'" class="commentcontent">
	<span onclick="viewcommentor()">'.$c_com[$c_id].'</span>
	</div>
	<div id="commenttime'.$c_id.'" class="dealtimerow">
	on '.$c_date[$c_id].' at .'.$c_time[$c_id].';
	</div>
	<hr>
	</div>';

	$c_id++;
	}


}

if($parameter == 'loadwishlist')
{
	
	$query= "SELECT * FROM user_fav where user_id = '".$curuser."'";
	$result= mysql_query($query) or die(mysql_error());	
	
	echo'<div id="mywishlist" class="box">         
      <h1>My Wish List</h1><br>
      <form action="" method="post" id="mywishlistform" >';
	$i=0;
	while($row = mysql_fetch_object($result))
	{
	
	$pid[$i] = $row->product_id;
	$ptime = $row->added_on;
	$addedon[$i] = $ptime;
	//echo $pid[$i];
	$query1= "SELECT * FROM product where product_id = '".$pid[$i]."'";
	$result1= mysql_query($query1) or die(mysql_error());	
	$row1 = mysql_fetch_object($result1);
	$pname[$i] = $row1->product_name;
	

	echo '
      <label class ="rowlabel" id="l'.$pid[$i].'"> 
      <span class="rowleft"><input type="checkbox" id="wp'.$pid[$i].'" value="'.$pname[$i].'" checked onchange="editwishlist(this.id)"/> </span>
      <input id="p'.$pid[$i].'" type="text" class="input_text" readonly="true" size="70"   value="'.$pname[$i].'" /><br /> 
      </label>
      ';

  	  $i++;
  	}

  	echo'<input type="submit" name="editsubmit" id="button" class ="decbutton" value="Submit for Update">
      </form>
  	  </div>';

}


if($parameter == 'editwishlist')
{
	
	$wpid = $_POST['wp'];

	$query= "DELETE FROM user_fav WHERE user_id = '".$curuser."' AND product_id = '".$wpid."'";
	$result= mysql_query($query) or die(mysql_error());

	//$e = mysql_num_rows($result);

}






?>
